package com.hyg.common.utils.glide;

import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import com.bumptech.glide.load.model.StringLoader;

import java.io.InputStream;

public class WrapperStringLoader<Data> extends StringLoader<Data> {



    @SuppressWarnings("WeakerAccess")
    public WrapperStringLoader(ModelLoader<Uri, Data> uriLoader) {
        super(uriLoader);
    }


    @Override
    public LoadData<Data> buildLoadData(@NonNull String model, int width, int height, @NonNull Options options) {
        return super.buildLoadData(model, width, height, options);
    }

    /**
     * Factory for loading {@link InputStream}s from Strings.
     */
    public static class StreamFactory implements ModelLoaderFactory<String, InputStream> {

        @NonNull
        @Override
        public ModelLoader<String, InputStream> build(
                @NonNull MultiModelLoaderFactory multiFactory) {
            return new WrapperStringLoader<>(multiFactory.build(Uri.class, InputStream.class));
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }

    /**
     * Factory for loading {@link ParcelFileDescriptor}s from Strings.
     */
    public static class FileDescriptorFactory
            implements ModelLoaderFactory<String, ParcelFileDescriptor> {

        @NonNull
        @Override
        public ModelLoader<String, ParcelFileDescriptor> build(
                @NonNull MultiModelLoaderFactory multiFactory) {
            return new WrapperStringLoader<>(multiFactory.build(Uri.class, ParcelFileDescriptor.class));
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }

    /**
     * Loads {@link AssetFileDescriptor}s from Strings.
     */
    public static final class AssetFileDescriptorFactory
            implements ModelLoaderFactory<String, AssetFileDescriptor> {

        @Override
        public ModelLoader<String, AssetFileDescriptor> build(
                @NonNull MultiModelLoaderFactory multiFactory) {
            return new WrapperStringLoader<>(multiFactory.build(Uri.class, AssetFileDescriptor.class));
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }

}
